use super::model::WechatCpMenuInfo;
use serde_json::Value;

use crate::wechat::cp::method::{CpMenuMethod, WechatCpMethod};
use crate::{
    request::RequestType, session::SessionStore, LabradorResult, WechatCommonResponse,
    WechatCpClient,
};

/// 菜单管理相关接口
#[derive(Debug, Clone)]
pub struct WechatCpMenu<'a, T: SessionStore> {
    client: &'a WechatCpClient<T>,
}

#[allow(unused)]
impl<'a, T: SessionStore> WechatCpMenu<'a, T> {
    #[inline]
    pub fn new(client: &WechatCpClient<T>) -> WechatCpMenu<T> {
        WechatCpMenu { client }
    }

    /// <pre>
    /// 自定义菜单创建接口
    /// 详情请见: <a href="http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口">文档</a>
    ///
    /// 注意: 这个方法使用配置里的agentId
    /// </pre>
    pub async fn create(&self, menu: WechatCpMenuInfo) -> LabradorResult<WechatCommonResponse> {
        self.create_with_agentid(self.client.agent_id.to_owned().unwrap_or_default(), menu).await
    }

    /// <pre>
    /// 自定义菜单创建接口
    /// 详情请见: <a href="http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口">文档</a>
    /// </pre>
    pub async fn create_with_agentid(
        &self,
        agent_id: i32,
        req: WechatCpMenuInfo,
    ) -> LabradorResult<WechatCommonResponse> {
        self.client
            .post(
                WechatCpMethod::Menu(CpMenuMethod::Create(agent_id)),
                vec![],
                req,
                RequestType::Json,
            )
            .await?
            .json::<WechatCommonResponse>()
    }

    /// <pre>
    /// 自定义菜单删除接口
    /// 详情请见: <a href="http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口">文档</a>
    ///
    /// 注意: 这个方法使用配置里的agentId
    /// </pre>
    pub async fn delete(&self) -> LabradorResult<WechatCommonResponse> {
        self.delete_with_agentid(self.client.agent_id.to_owned().unwrap_or_default()).await
    }

    /// <pre>
    /// 自定义菜单删除接口
    /// 详情请见: <a href="http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口">文档</a>
    /// </pre>
    pub async fn delete_with_agentid(&self, agent_id: i32) -> LabradorResult<WechatCommonResponse> {
        self.client
            .post(
                WechatCpMethod::Menu(CpMenuMethod::Delete(agent_id)),
                vec![],
                Value::Null,
                RequestType::Json,
            )
            .await?
            .json::<WechatCommonResponse>()
    }

    /// <pre>
    /// 自定义菜单查询接口
    /// 详情请见: <a href="http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口">文档</a>
    ///
    /// 注意: 这个方法使用配置里的agentId
    /// </pre>
    pub async fn get(&self) -> LabradorResult<WechatCommonResponse> {
        self.delete_with_agentid(self.client.agent_id.to_owned().unwrap_or_default()).await
    }

    /// <pre>
    /// 自定义菜单查询接口
    /// 详情请见: <a href="http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口">文档</a>
    /// </pre>
    pub async fn get_with_agentid(&self, agent_id: i32) -> LabradorResult<WechatCpMenuInfo> {
        let v = self
            .client
            .post(
                WechatCpMethod::Menu(CpMenuMethod::Get(agent_id)),
                vec![],
                Value::Null,
                RequestType::Json,
            )
            .await?
            .json::<Value>()?;
        WechatCommonResponse::parse::<WechatCpMenuInfo>(v)
    }
}

//----------------------------------------------------------------------------------------------------------------------------
